package com.sdp.wx.controller;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_vehicle_item.entity.WxRepairVehicleItem;
import com.sdp.wx.db.wx_repair_vehicle_item.service.IWxRepairVehicleItemService;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-18
 */
@RestController
@RequestMapping("/WxRepairVehicleItemController")
public class WxRepairVehicleItemController {

	@Autowired
	private IWxRepairVehicleItemService iWxRepairVehicleItemService;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairVehicleItemService, WxRepairVehicleItem.class, dataCenter));
	}
	
	/**
	 * 添加维修项目
	 * @param items
	 * @return
	 */
	@PostMapping(value = "/addItem")
	@Transactional(rollbackFor=Exception.class)
	public synchronized Result<?> addItem(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("维修项目信息不存在");
		}
		List<WxRepairVehicleItem> items = JSONArray.parseArray(JSON.toJSONString(obj.get("itemList")), WxRepairVehicleItem.class);
		String repairCode = obj.getString("repairCode");
		BigDecimal hourQuota = obj.getBigDecimal("hourQuota");
		BigDecimal hourFactor = obj.getBigDecimal("hourFactor");
		if(StringUtils.isEmpty(repairCode)) {
			return Result.error("维修编号不存在");
		}
		if(hourQuota==null) {
			return Result.error("工时定额不存在");
		}
		if(hourFactor==null) {
			return Result.error("工时系数不存在");
		}
		if(items==null || items.size()==0) {
			return Result.error("维修项目信息不存在");
		}
		for(WxRepairVehicleItem item : items) {
			item.setKeyId(null);
			item.setRepairCode(repairCode);
			item.setHourFactor(hourFactor);
			item.setHourQuota(hourQuota);
			item.setHoutAmount(item.getItemHour().multiply(hourQuota).multiply(hourFactor).setScale(2,BigDecimal.ROUND_HALF_UP));
		}
		List<WxRepairVehicleItem> dbItems = iWxRepairVehicleItemService.queryWxRepairVehicleItemByRepairCode(repairCode);
		if(dbItems==null || dbItems.size()==0) {
			iWxRepairVehicleItemService.saveBatch(items);
		}else {
			Iterator<WxRepairVehicleItem> iterator = items.iterator();
			for(WxRepairVehicleItem dbItem : dbItems) {
				if(items.size()==0) {
					break;
				}
				iterator = items.iterator();
				while (iterator.hasNext()) {
					if(iterator.next().getItemNo().equals(dbItem.getItemNo())) {
						iterator.remove();
						break;
					}
				}
			}
			
			if(items.size()>0) {
				iWxRepairVehicleItemService.saveBatch(items);
			}
		}
		return Result.ok();
	}
	
	/**
	 * 派工
	 * @param obj
	 * @return
	 */
	@PostMapping(value = "/dispatch")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> dispatch(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("派工信息不存在");
		}
		List<String> items = JSONArray.parseArray(JSON.toJSONString(obj.get("keyIds")), String.class);
		WxRepairVehicleItem wxRepairVehicleItem = obj.getObject("record", WxRepairVehicleItem.class);
		if(wxRepairVehicleItem==null) {
			return Result.error("派工信息不存在");
		}
		
		if(items!=null && items.size()>0) {
			LambdaUpdateWrapper<WxRepairVehicleItem> updateWrapper = new LambdaUpdateWrapper<>();
			updateWrapper.set(WxRepairVehicleItem::getRepairWorker, wxRepairVehicleItem.getRepairWorker())
				.set(WxRepairVehicleItem::getRepairWorkerName, wxRepairVehicleItem.getRepairWorkerName())
				.in(WxRepairVehicleItem::getKeyId, items);
			iWxRepairVehicleItemService.update(updateWrapper);
		}else {
			iWxRepairVehicleItemService.updateById(wxRepairVehicleItem);
		}
		
		return Result.ok();
	}
	
	/**
	 * 根据项目类型派工
	 * @param obj
	 * @return
	 */
	@PostMapping(value = "/itemTypeDispatch")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> itemTypeDispatch(@RequestBody WxRepairVehicleItem wxRepairVehicleItem) {
		if(wxRepairVehicleItem==null){
			return Result.error("派工信息不存在");
		}
		
		LambdaUpdateWrapper<WxRepairVehicleItem> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(WxRepairVehicleItem::getRepairWorker, wxRepairVehicleItem.getRepairWorker())
			.set(WxRepairVehicleItem::getRepairWorkerName, wxRepairVehicleItem.getRepairWorkerName())
			.eq(WxRepairVehicleItem::getRepairCode, wxRepairVehicleItem.getRepairCode())
			.eq(WxRepairVehicleItem::getItemType, wxRepairVehicleItem.getItemType());
		iWxRepairVehicleItemService.update(updateWrapper);
		return Result.ok();
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxRepairVehicleItemService.removeById(keyId);
		return Result.ok();
	}
}

